rm(list=ls()) #clear

library(summarytools)
library(dplyr)
library(stargazer)
library(psych)

std01<-function(x){
  min.x<-min(x, na.rm=T)
  max.x<-max(x-min.x, na.rm=T)
  return((x-min.x)/max.x)
} #recode vars 0 to 1

#importing data from Qualtrics and demographic data from Prolific
Qual <- read.csv("Study 1 - Prolific/Qual_S1.csv")
Demo_REP <- read.csv("Study 1 - Prolific/Demo_Rep_S1.csv")                     
Demo_DEM <- read.csv("Study 1 - Prolific/Demo_Dem_S1.csv")

#merge the data using the participant's Prolific ID
datraw<-merge(Qual, Demo_REP, by="participant_id", all = "TRUE")
dat<-merge(datraw, Demo_DEM, by="participant_id", all = "TRUE")

#removing disqualified
dat<- subset(dat, dat$Finished==1)
dat<- subset(dat, dat$Consent.Form==1)

#which form participant received
dat$pid<- ifelse(!is.na(dat$U.S..Political.Affiliation.x), "Rep", "Dem") 
dat$pid<- as.factor(dat$pid)

dat[paste0("o", 1:15)] <- NA #new vars to hold both forms
for(i in 1:15){
  f <- paste0("o", i)       # new merged vars
  df <- paste0("do", i)     # dem variables
  rf <- paste0("ro", i)     # rep variables
  dat[,f][which(dat$pid=="Dem")]<-dat[,df][which(dat$pid=="Dem")]
  dat[,f][which(dat$pid=="Rep")]<-dat[,rf][which(dat$pid=="Rep")]
} #othering items

dat[paste0("a", 1:15)] <- NA #new vars to hold both forms
for(i in 1:15){
  f <- paste0("a", i)       # new merged vars  
  df <- paste0("da", i)     # dem variables
  rf <- paste0("ra", i)     # rep variables
  dat[,f][which(dat$pid=="Dem")]<-dat[,df][which(dat$pid=="Dem")]
  dat[,f][which(dat$pid=="Rep")]<-dat[,rf][which(dat$pid=="Rep")]
}#aversion items

dat[paste0("m", 1:15)] <- NA #new vars to hold both forms
for(i in 1:15){
  f <- paste0("m", i)       # new merged vars
  df <- paste0("dm", i)     # dem variables
  rf <- paste0("rm", i)     # rep variables
  dat[,f][which(dat$pid=="Dem")]<-dat[,df][which(dat$pid=="Dem")]
  dat[,f][which(dat$pid=="Rep")]<-dat[,rf][which(dat$pid=="Rep")]
}#moralization items

dat[paste0("id", 1:4)] <- NA #new vars to hold both forms
for(i in 1:4){
  f <- paste0("id", i)       # new merged vars
  df <- paste0("did", i)     # dem variables
  rf <- paste0("rid", i)     # rep variables
  dat[,f][which(dat$pid=="Dem")]<-dat[,df][which(dat$pid=="Dem")]
  dat[,f][which(dat$pid=="Rep")]<-dat[,rf][which(dat$pid=="Rep")]
}#huddy et. al. items

#cleaning 3,4,13  5,7,9,10,14   14
dat<- dat %>% 
  mutate_at(c("o3", "o4", "o13",
              "a5", "a7", "a9", "a10", "a14",
              "m14"),
            funs(dplyr::recode(., "1"=7, "2"=6, "3"=5, "4"=4, "5"=3, "6"=2, "7"=1))) #recoding reverse coded items
dat<- dat %>%
  mutate_at(c("id1", "id2", "id4"),
            funs(dplyr::recode(., "1"=4, "2"=3, "3"=2, "4"=1)))
dat<- dat %>%
  mutate_at(c("id3"),
            funs(dplyr::recode(., "1"=5, "2"=4, "3"=3, "4"=2, "5"=1))) #reverse coding Huddy et. al. items

##### For Post-Factor Analysis #####

#changing to 01 coding for analysis
dat$pid01<- ifelse(dat$pid == "Dem", 0, 1)

#aps
dat$otot20<-rowMeans(with(dat, cbind(o1, o7, o8, o9, o10, o12, o14, o15)))
dat$atot20<-rowMeans(with(dat, cbind(a3, a4, a9, a10, o4, m9)))
dat$mtot20<-rowMeans(with(dat, cbind(m1, m2, m3, m4, m5, m6)))
dat$apstot20<-rowMeans(with(dat, cbind(o1, o7, o8, o9, o10, o12, o14, o15,
                                          a3, a4, a9, a10, o4, m9,
                                          m1, m2, m3, m4, m5, m6)))

dat$otot9<-rowMeans(with(dat, cbind(o1, o12, o15)))
dat$atot9<-rowMeans(with(dat, cbind(a3, a4, a10)))
dat$mtot9<-rowMeans(with(dat, cbind(m2, m4, m6)))
dat$apstot9<-rowMeans(with(dat, cbind(o1, o12, o15,
                                      a3, a4, a10,
                                      m2, m4, m6)))
descr(dat$apstot9)
descr(dat$otot9)
descr(dat$atot9)
descr(dat$mtot9)

#ideology
dat$rideo7<-(dat$ideo7-1)/6

#ideological extremity, 0-3
dat$idex<-abs(dat$ideo7-4)
#ideological extremity, 0-1
dat$ridex<-std01(dat$idex)
#ideological extremity, 0-3, factor variable.
dat$fidex<-as.factor(dat$idex)

#Huddy et. al. PID
psych::alpha(with(dat, cbind(id1, id2, id3, id4))) #.85
dat$idtot<-rowMeans(with(dat, cbind(id1, id2, id3, id4)))

# party ID extremity (RELABELED)
# 0, 1, 2 coding:
dat$pidex<- ifelse(dat$pid7==1 | dat$pid7==7, 2, 
                      ifelse(dat$pid7==2 | dat$pid7==6, 1,
                             ifelse(dat$pid7==3 | dat$pid7==5, 0, NA)))
# 0, 0.5, 1 coding:
dat$rpidex<-std01(dat$pidex)
# make 0, 1, 2 coding a factor variable - we need this to run ordered logit below:
dat$fpidex<-as.factor(dat$pidex)

#recoding demographic vars
dat$sex<-ifelse(!is.na(dat$Sex.x), dat$Sex.x, dat$Sex.y)
dat$male<- ifelse(dat$sex == "Male", 1, 0)

dat$age<-ifelse(!is.na(dat$age.x), dat$age.x, dat$age.y)

dat$raceshort<-ifelse(!is.na(dat$Ethnicity..Simplified..x), dat$Ethnicity..Simplified..x, dat$Ethnicity..Simplified..y)
dat$white<- ifelse(dat$raceshort == "White", 1, 0)

dat$racelong<-ifelse(!is.na(dat$Ethnicity.x), dat$Ethnicity.x, dat$Ethnicity.y)
dat$hisplat<- ifelse(dat$racelong == "Latino/Hispanic" | dat$racelong == "White Mexican", 1, 0)

dat$education<-ifelse(!is.na(dat$Highest.education.level.completed.x), dat$Highest.education.level.completed.x, dat$Highest.education.level.completed.y)
dat$ba<- ifelse(dat$education == "Undergraduate degree (BA/BSc/other)" | 
                   dat$education == "Graduate degree (MA/MSc/MPhil/other)" |
                   dat$education == "Doctorate degree (PhD/other)", 1, 
                 ifelse(dat$education == "No formal qualifications" | 
                          dat$education == "Don't know / not applicable" |
                          dat$education == "High school diploma/A-levels" |
                          dat$education == "Secondary education (e.g. GED/GCSE)" |
                          dat$education == "Technical/community college", 0, NA))

dat$income<-ifelse(!is.na(dat$Household.Income..USD...US.participants.only..x), dat$Household.Income..USD...US.participants.only..x, dat$Household.Income..USD...US.participants.only..y)
dat$inc<- ifelse(dat$income == "Less than $10000", 1,
                 ifelse(dat$income == "$10000–$15999", 2,
                        ifelse(dat$income == "$16000–$19999", 3,
                               ifelse(dat$income == "$20000–$29999", 4,
                                      ifelse(dat$income == "$30000–$39999", 5,
                                             ifelse(dat$income == "$40000–$49999", 6,
                                                    ifelse(dat$income == "$50000–$59999", 7,
                                                           ifelse(dat$income == "$60000–$69999", 8,
                                                                  ifelse(dat$income == "$70000–$79999", 9,
                                                                         ifelse(dat$income == "$80000–$89999", 10,
                                                                                ifelse(dat$income == "$90000–$99999", 11,
                                                                                       ifelse(dat$income == "$100000–$149999", 12,
                                                                                              ifelse(dat$income == "More than $150000", 13, NA)))))))))))))
table(dat$raceshort)
summary(dat$age)
sd(dat$age)

table(dat$sex)
table(dat$ba)
table(dat$white)
table(dat$hisplat)
table(dat$inc)

dat<- dat %>%
  mutate_at(c("apstot20", "otot20", "atot20", "mtot20",
              "apstot9", "otot9", "atot9", "mtot9", "idtot",
              "age", "inc"),
            funs(std01(.)))

dat<- dat[c("o1", "o2", "o3", "o4", "o5", "o6", "o7", "o8", "o9", "o10", "o11", "o12",
            "o13", "o14", "o15", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9",
            "a10", "a11", "a12", "a13", "a14", "a15", "m1", "m2", "m3", "m4", "m5", "m6",
            "m7", "m8", "m9", "m10", "m11", "m12", "m13", "m14", "m15", "otot20", 
            "atot20", "mtot20", "apstot20", "otot9", "atot9", "mtot9", "apstot9", "pid",
            "pid01", "idtot", "rpidex", "fpidex", "rideo7", "ridex", "fidex",
            "male", "age", "white", "hisplat", "ba", "inc")]

save(dat, file = "Study 1 - Prolific/dataS1.RData")

dat1<- dat
save(dat1, file = "Main Analyses/dataS1.RData")

